<template>
  <div class="person-center">
    <div class="person-top">
      <el-card class="person_top_card">
        <div class="PersonTop">
          <div class="PersonTop_img">
            <!-- <img v-image-preview :src="user.avatar" /> -->
            <el-avatar :size="120" :src="user.avatar" />
          </div>
          <div class="PersonTop_text">
            <div class="user_text">
              <div class="user_name">
                <span> {{ user.username }} </span>
              </div>
              <div class="user_qianming">
                <span> {{ user.design }}</span>
              </div>
              <div class="user_anniu">
                <el-button
                  class="el-icon-edit"
                  v-if="
                    this.$route.params.id ===
                    this.$store.state.user.userInfo.userId
                  "
                  type="primary"
                  size="medium"
                  plain
                  @click="edit"
                  >编辑</el-button
                >
                <div v-else>
                  <el-button
                    v-if="user.isFollowed"
                    class="el-icon-follow"
                    @click="cancelFollow(user.id)"
                    type="primary"
                    size="medium"
                    icon="el-icon-check"
                    plain
                    >已关注</el-button
                  >

                  <el-button
                    v-else
                    class="el-icon-follow"
                    @click="follow(user.id)"
                    type="primary"
                    size="medium"
                    icon="el-icon-check"
                    plain
                    >关注</el-button
                  >
                </div>
              </div>
            </div>
            <div class="user_num">
              <div style="cursor: pointer" @click="myfan">
                <div class="num_number">{{ user.fanCount }}</div>
                <span class="num_text">粉丝</span>
              </div>
              <div style="cursor: pointer" @click="myfollow">
                <div class="num_number">{{ user.followCount }}</div>
                <span class="num_text">关注</span>
              </div>
              <div>
                <div class="num_number">{{ user.goodCount }}</div>
                <span class="num_text">获赞</span>
              </div>
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <div class="person_body">
      <div class="person_body_left">
        <el-card class="box-card" :body-style="{ padding: '0px' }">
          <div class="clearfix">
            <span class="person_body_list" style="border-bottom: none"
              >个人中心</span
            >
          </div>
          <el-menu
            router
            active-text-color="#00c3ff"
            class="el-menu-vertical-demo"
          >
            <!-- <el-menu-item index="/home/userCenter/info">
              <i class="el-icon-user"></i>
              <span>个人简介</span>
            </el-menu-item> -->
            <el-menu-item
              :index="'/home/userCenter/' + user.id + '/myComments'"
            >
              <i class="el-icon-edit-outline"></i>
              <span
                :style="{
                  color: currentRoute === 'myComments' ? conFontColor : '#000',
                }"
                ><el-icon><Comment /></el-icon>我的评论</span
              >
            </el-menu-item>
            <el-menu-item :index="'/home/userCenter/' + user.id + '/myFans'">
              <i class="el-icon-tableware"></i>
              <span
                :style="{
                  color: currentRoute === 'myFans' ? conFontColor : '#000',
                }"
                ><el-icon><Avatar /></el-icon>我的粉丝</span
              >
            </el-menu-item>
            <el-menu-item :index="'/home/userCenter/' + user.id + '/myFollows'">
              <i class="el-icon-circle-plus-outline"></i>
              <span
                :style="{
                  color: currentRoute === 'myFollows' ? conFontColor : '#000',
                }"
                ><el-icon><StarFilled /></el-icon>我的关注</span
              >
            </el-menu-item>
          </el-menu>
        </el-card>
      </div>
      <div class="person_body_right">
        <el-card :header="header">
          <div class="person_body_right_content">
            <router-view></router-view>
          </div>
        </el-card>
      </div>
    </div>
    <!-- <personal-dia ref="dia" @flesh="reload" /> -->
  </div>
</template>

<script>
import { cancelFollow, follow } from "@/api/userCenter";
import { ElMessage } from "element-plus";

export default {
  name: "PersonalView",
  inject: ["reload"],
  props: {
    user: {
      type: Object,
      required: true,
      default: () => ({}),
    },
  },
  data() {
    return {
      header: "",
      currentRoute: "",
      conFontColor: "red",
    };
  },
  mounted() {
    this.load();
  },
  watch: {
    $route(to) {
      console.log(to.path.split("/")[4]);
      this.updateCardTitle(to.path.split("/")[4]);
      this.currentRoute = to.path.split("/")[4];
    },
  },
  methods: {
    updateCardTitle(title) {
      switch (title) {
        case "info":
          this.header = "个人简介";
          break;
        case "myComments":
          this.header = "我的评论";
          break;
        case "myFans":
          this.header = "我的粉丝";
          break;
        case "myFollows":
          this.header = "我的关注";
          break;
      }
    },
    load() {
      this.updateCardTitle(
        this.$router.currentRoute.value.fullPath.split("/")[4]
      );
      this.currentRoute =
        this.$router.currentRoute.value.fullPath.split("/")[4];
    },
    myFan() {},
    myFollow() {},

    // 关注当前用户
    async follow(id) {
      const res = await follow(id);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "关注成功",
        });
        location.reload();
      }
    },

    // 取消关注当前用户
    async cancelFollow(id) {
      const res = await cancelFollow(id);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "取消关注成功",
        });
        location.reload();
      }
    },
    edit() {
      this.$router.push({
        path: "/home/userCenter/edit",
        props: {
          userInfo: this.user,
        },
      });
    },
  },
};
</script>

<style scoped>
.person-top {
  display: flex;
  justify-content: center;
  width: 1500px;
  margin-top: 20px;
}
.me-video-player {
  background-color: transparent;
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  position: fixed;
  left: 0;
  z-index: 0;
  top: 0;
}
.PersonTop {
  width: 890px;
  height: 140px;
  padding-top: 10px;
  background-color: white;
  margin-top: 30px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  border-radius: 5px;
}
.person-top_card {
  width: 1000px;
}

.PersonTop_img {
  width: 150px;
  height: 120px;
  margin-right: 24px;
  margin-left: 20px;
  overflow: hidden;
  border-radius: 20px;
}

.PersonTop_img img {
  width: 100%;
  height: 100%;
  border-radius: 20px;
}

.PersonTop_text {
  height: 120px;
  width: 880px;
  display: flex;
}

.user_text {
  width: 60%;
  height: 100%;
  line-height: 30px;
}

.user_name {
  font-weight: bold;
}
.user-v {
  margin-bottom: -5px;
}
.user-v-img {
  width: 15px;
  height: 15px;
}
.user-v-font {
  font-size: 15px;
  color: #00c3ff;
}
.user_qianming {
  font-size: 14px;
  color: #999;
}

.user_num {
  width: 40%;
  height: 100%;
  display: flex;
  align-items: center;
}

.user_num > div {
  text-align: center;
  border-right: 1px dotted #999;
  box-sizing: border-box;
  width: 80px;
  height: 40px;
  line-height: 20px;
}

.num_text {
  color: #999;
}

.num_number {
  font-size: 20px;
  color: #333;
}
.el-menu-item > span {
  font-size: 16px;
  color: #999;
}

/*下面部分样式*/
.person_body {
  width: 1000px;
  margin-top: 50px;
  display: flex;
  position: relative;
  left: 52%;
  transform: translateX(-50%);
  border-radius: 5px;
}

.person_body_left {
  width: 20%;
  height: 400px;
  border-radius: 5px;
  margin-right: 3%;
  text-align: center;
}

.person_body_list {
  width: 100%;
  height: 50px;
  margin-top: 25px;
  font-size: 22px;
  border-bottom: 1px solid #f0f0f0;
  background-image: -webkit-linear-gradient(
    left,
    rgb(42, 134, 141),
    #e9e625dc 20%,
    #3498db 40%,
    #e74c3c 60%,
    #09ff009a 80%,
    rgba(82, 196, 204, 0.281) 100%
  );
  -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;
  -webkit-background-size: 200% 100%;
  -webkit-animation: masked-animation 4s linear infinite;
}

.el-menu-item {
  margin-top: 22px;
}

.person_body_right {
  width: 70%;
  height: 700px;
  border-radius: 5px;
  background-color: rgb(255, 255, 255);
}
.person_body_right_content {
  height: 600px;
}

.box-card {
  height: 360px;
}

/*ui样式*/
.el-button {
  width: 84px;
}
.footer {
  position: absolute;
  bottom: 0;
}
</style>
